﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/21/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoDocument
    {
        public daoDocument()
        {

        }
        
        private Document Load(IDataReader de)
        {
            Document item = new Document();
            item.DocumentID = clsConvert.DbToInt("DocumentID", de);
            item.FileName = clsConvert.DbToString("FileName", de);          
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.Size = clsConvert.DbToString("Size", de);
            item.Color = clsConvert.DbToString("Color", de);
            item.Material = clsConvert.DbToString("Material", de);          
            item.SortOrder = clsConvert.DbToInt("SortOrder", de);           
            item.CreatedBy = clsConvert.DbToString("CreatedBy", de);
            item.CreatedDate = clsConvert.DbToDateTime("CreatedDate", de);
            item.UpdatedBy = clsConvert.DbToString("UpdatedBy", de);
            item.UpdatedDate = clsConvert.DbToDateTime("UpdatedDate", de);
            return item;           
        }
        public int Insert(Document objDocument)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[8];
            try
            {
                para[0] = new SqlParameter(busDocument.NAME, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objDocument.Name);
                para[1] = new SqlParameter(busDocument.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objDocument.IsActive;
                para[2] = new SqlParameter(busDocument.FILENAME, SqlDbType.NVarChar);
                para[2].Value = objDocument.FileName;
                para[3] = new SqlParameter(busDocument.CREATEDBY, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objDocument.CreatedBy);
                para[4] = new SqlParameter(busDocument.CREATEDDATE, SqlDbType.DateTime);
                para[4].Value = objDocument.CreatedDate;
                para[5] = new SqlParameter(busDocument.SIZE, SqlDbType.NVarChar);
                para[5].Value = clsConvert.StringToDB(objDocument.Size);
                para[6] = new SqlParameter(busDocument.COLOR, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objDocument.Color);
                para[7] = new SqlParameter(busDocument.MATERIAL, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objDocument.Material);
                result = clsSQL.SPExecute("Document_Insert", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "Insert(Document objDocument)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public bool DeActive(int documentID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                result = clsSQL.SPExecute("Document_DeActive", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "DeActive(int documentID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Active(int documentID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                result = clsSQL.SPExecute("Document_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "Active(int documentID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int documentID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                result = clsSQL.SPExecute("Document_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "Delete(int documentID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public Document GetByID(int documentID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Document item = new Document();
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                using (de = clsSQL.SPDataReader("Document_GetByID", para))
                {
                    while (de.Read())
                    {
                        item = Load(de);
                    }

                    de.Close();
                    de.Dispose();
                }
            }
            catch (Exception ex)
            {
                item = null;
                clsFile.WriteLog("daoDocument", " GetByID(int documentID)", ex.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return item;
        }
        public bool Update(Document objDocument)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[9];
            try
            {
                para[0] = new SqlParameter(busDocument.NAME, SqlDbType.NVarChar);
                para[0].Value = clsConvert.StringToDB(objDocument.Name);
                para[1] = new SqlParameter(busDocument.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objDocument.IsActive;
                para[2] = new SqlParameter(busDocument.FILENAME, SqlDbType.NVarChar);
                para[2].Value = objDocument.FileName;
                para[3] = new SqlParameter(busDocument.UPDATEDBY, SqlDbType.NVarChar);
                para[3].Value = clsConvert.StringToDB(objDocument.UpdatedBy);
                para[4] = new SqlParameter(busDocument.UPDATEDDATE, SqlDbType.DateTime);
                para[4].Value = objDocument.UpdatedDate;
                para[5] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[5].Value = objDocument.DocumentID;
                para[6] = new SqlParameter(busDocument.SIZE, SqlDbType.NVarChar);
                para[6].Value = clsConvert.StringToDB(objDocument.Size);
                para[7] = new SqlParameter(busDocument.COLOR, SqlDbType.NVarChar);
                para[7].Value = clsConvert.StringToDB(objDocument.Color);
                para[8] = new SqlParameter(busDocument.MATERIAL, SqlDbType.NVarChar);
                para[8].Value = clsConvert.StringToDB(objDocument.Material);
                result = clsSQL.SPExecute("Document_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "Update(Document objDocument)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public DocumentCollection List(Document objDocument)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            DocumentCollection items = new DocumentCollection();

            try
            {
                para[0] = new SqlParameter(busDocument.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objDocument.OrderBy;
                para[1] = new SqlParameter(busDocument.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objDocument.OrderDirection;
                para[2] = new SqlParameter(busDocument.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objDocument.CurrentPage;
                para[3] = new SqlParameter(busDocument.PAGESIZE, SqlDbType.Int);
                para[3].Value = objDocument.PageSize;
                para[4] = new SqlParameter(busDocument.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objDocument.Condition;
                para[5] = new SqlParameter(busDocument.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Document_List", para);
                while (de.Read())
                {
                    Document item = Load(de);
                    items.Add(item);
                }

                de.Close();
                objDocument.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoDocument", "List(Document objDocument)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public bool OrderDown(int documentID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                result = clsSQL.SPExecute("Document_OrderDown", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "OrderDown(int documentID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool OrderUp(int documentID)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busDocument.DOCUMENTID, SqlDbType.Int);
                para[0].Value = documentID;
                result = clsSQL.SPExecute("Document_OrderUp", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoDocument", "OrderUp(int documentID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public DocumentCollection GetListByProductID(Document objDocument)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            DocumentCollection items = new DocumentCollection();
            try
            {
              
                para[0] = new SqlParameter(busDocument.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = objDocument.OrderBy;
                para[1] = new SqlParameter(busDocument.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = objDocument.OrderDirection;
                para[2] = new SqlParameter(busDocument.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = objDocument.CurrentPage;
                para[3] = new SqlParameter(busDocument.PAGESIZE, SqlDbType.Int);
                para[3].Value = objDocument.PageSize;               
                para[4] = new SqlParameter(busDocument.CONDITION, SqlDbType.NVarChar);
                para[4].Value = objDocument.Condition;
                para[5] = new SqlParameter(busDocument.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Document_GetListByProductID", para);
                while (de.Read())
                {
                    Document item = Load(de);
                    items.Add(item);
                }

                de.Close();              
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoDocument", "GetListByProductID(int productID)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }

    }
}

